<html>
<head><meta charset="utf-8"><title>exact `async` fn desugaring · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html">exact `async` fn desugaring</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205437843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205437843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205437843">(Jul 30 2020 at 03:13)</a>:</h4>
<p>Hi all! Is there a specification on exactly how <code>async fn</code>s are desugared, especially with respect to lifetimes? I'm hitting what appears to be <a href="https://github.com/rust-lang/rust/issues/71723">#71723</a>. Here's a <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=28fc6de0e9243cf42ef9143d960c8edf">playground</a> with an example: the last <code>check</code> doesn't compile due to, it seems, the way lifetimes make their way into the desugared version of the function.</p>



<a name="205438693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205438693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205438693">(Jul 30 2020 at 03:35)</a>:</h4>
<p>They aren't really desugared, they're lowered directly into HIR (which you can print with <code>-Zunpretty=hir</code>, but it probably won't be super insightful)</p>



<a name="205438756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205438756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205438756">(Jul 30 2020 at 03:37)</a>:</h4>
<p>I'm afraid I can't dig in at the moment, but please comment on that issue since it will help us prioritize it.</p>



<a name="205438897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205438897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205438897">(Jul 30 2020 at 03:41)</a>:</h4>
<p>Indeed, the HIR doesn't provide much of an insight, but the MIR might! Thank you for the idea.</p>
<p>I suppose I'm more looking for documentation on what <code>async fn</code> is _supposed_ to do with lifetimes, not necessarily what the equivalent desugaring would be.</p>



<a name="205439612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205439612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205439612">(Jul 30 2020 at 03:57)</a>:</h4>
<p>This appears to be really be <a href="https://github.com/rust-lang/rust/issues/64552">#64552</a>, which I've commented in.</p>



<a name="205489557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205489557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205489557">(Jul 30 2020 at 15:01)</a>:</h4>
<p>that's a thorny one</p>



<a name="205637256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205637256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205637256">(Jul 31 2020 at 20:19)</a>:</h4>
<p><span class="user-mention" data-user-id="326186">@Sergio Benitez</span> Regarding your example:</p>
<div class="codehilite"><pre><span></span><code>async fn _handler3&lt;&#39;r&gt;(request: &amp;&#39;r Request&lt;&#39;_&gt;) -&gt; Response&lt;&#39;r&gt; {
        Response(&amp;request.0)
    }
// check(_handler3); // FIXME: Why doesn&#39;t this work? What lifetimes does the generated `Future` have?
</code></pre></div>


<p>Those are <strong>really</strong> tricky! The returned <code>Future</code>s will always have a different lifetime than what they store. It obviously needs to be shorter, since otherwise the Future could outlive the lifetime of its content. But it's really hard to refer to those lifetime in applications in a way which doesn't end up in trial and error. </p>
<p>Unfortunately I have no real hint advise on how to deal with it apart from "do try and error", and "try to minimize lifetimes" :'(. I got my stuff working, but had at some times to rethink things</p>



<a name="205637430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205637430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205637430">(Jul 31 2020 at 20:21)</a>:</h4>
<p>I think the fact that lifetimes are getting a lot more complicated is a consequence of Rust <code>Future</code>s  being lazy that we might not have thought a lot about early on, since <code>Future</code>s 0.1 didn't support lifetimes anyway. But when trying to use <code>async/await</code> in a way that resembles normal function it gets more obvious</p>



<a name="205672643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205672643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205672643">(Aug 01 2020 at 09:02)</a>:</h4>
<p><span class="user-mention" data-user-id="204219">@Matthias247</span> What I'm most surprised by is that the inferred/generated lifetimes differ between the <code>async fn() -&gt; T { ... }</code> version and the <code>fn() - &gt; impl Future&lt;Output = T&gt; { move async { ... } }</code> version.</p>



<a name="205672711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/exact%20%60async%60%20fn%20desugaring/near/205672711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sergio <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/exact.20.60async.60.20fn.20desugaring.html#205672711">(Aug 01 2020 at 09:04)</a>:</h4>
<p>As a first approximation of a mental model, I would posit that these two forms would be equivalent, up to being able to 1) specify a lifetime bound on <code>Future</code>, and 2) run something before generating the <code>Future</code>. In my example, neither occurs, and yet a compile-determining difference remains.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>